package com.zryc.shop.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.pms.entity.ProductCategory;
import com.zryc.shop.pms.service.IProductCategoryService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/pms/productCategory")
@Api(tags = "商品类别管理接口列表")
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService categoryService;//商品类别

    @GetMapping("/list/{parentId}")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@PathVariable @ApiParam(value = "父类别ID", required = true) Long parentId,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        categoryService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("创建商品类别")
    public ResponseResult create(@RequestBody ProductCategory category) {
        if (category.getParentId()==0) //说明是顶级分类
            category.setLevel(0);
        else
            category.setLevel(1);
        categoryService.save(category);
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除商品类别")
    public ResponseResult delete(@PathVariable @ApiParam(value = "类别ID", required = true) Long id) {
        categoryService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询商品类别详情")
    public ResponseResult detail(@PathVariable @ApiParam(value = "类别ID", required = true) Long id) {
        return new ResponseResult(200, "查询完成", categoryService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改商品类别")
    public ResponseResult update(@PathVariable @ApiParam(value = "类别ID", required = true) Long id, @RequestBody ProductCategory category) {
        category.setId(id);
        if (category.getParentId()==0) //说明是顶级分类
            category.setLevel(0);
        else
            category.setLevel(1);
        categoryService.updateById(category);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/showStatus")
    @ApiOperation("修改是否显示")
    public ResponseResult updateShowStatus(@RequestParam @ApiParam(value = "类别ID", required = true) String ids,
                                           @RequestParam @ApiParam(value = "显示状态", required = true, allowableValues = "0,1") Integer showStatus) {
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", ids);
        updateWrapper.set("show_status", showStatus);
        categoryService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/navStatus")
    @ApiOperation("修改是否显示在导航栏")
    public ResponseResult updateNavStatus(@RequestParam @ApiParam(value = "类别ID", required = true) String ids,
                                          @RequestParam @ApiParam(value = "导航栏显示状态", required = true, allowableValues = "0,1") Integer navStatus) {
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", ids);
        updateWrapper.set("nav_status", navStatus);
        categoryService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }


    @GetMapping("/list/withChildren")
    @ApiOperation("查询商品类别（带子类别）")
    public ResponseResult listWithChildren() {
        return new ResponseResult(200, "查询完成", categoryService.listWithChildren());
    }





}
